package com.mdinfo.system.service.impl;

import com.mdinfo.base.common.bean.CommonResult;
import com.mdinfo.base.common.bean.DataGrid;
import com.mdinfo.base.common.bean.MemberInfo;
import com.mdinfo.base.common.bean.Ref;
import com.mdinfo.base.utils.SystemUtil;
import com.mdinfo.base.utils.TimeUtil;
import com.mdinfo.system.bean.SysDept;
import com.mdinfo.system.bean.SysEmp;
import com.mdinfo.system.bean.SysMember;
import com.mdinfo.system.mapper.SysDeptMapper;
import com.mdinfo.system.mapper.SysEmpMapper;
import com.mdinfo.system.mapper.SysMemberMapper;
import com.mdinfo.system.param.EmpParam;
import com.mdinfo.system.service.IEmpService;
import com.mdinfo.system.vo.EmpVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/11/30.
 */
@Service
@Transactional
public class EmpServiceImpl implements IEmpService {

    @Resource
    private SysEmpMapper empMapper;
    @Resource
    private SysDeptMapper deptMapper;
    @Resource
    private SysMemberMapper memberMapper;

    /**
     * 新增人员
     */
    public CommonResult<Void> add(EmpParam.AddParam param){

        SysDept dept = deptMapper.selectById(param.getDeptId());
        if(dept==null){
            return Ref.error("错误的部门编号");
        }

        SysEmp emp = new SysEmp();
        emp.setCreateTime(new Date());
        emp.setDeptId(param.getDeptId());
        emp.setName(param.getName());
        emp.setPhoneNum(param.getPhoneNum());
        emp.setPost(param.getPost());
        empMapper.insert(emp);
        return Ref.ok();
    }

    /**
     * 修改人员
     */
    public CommonResult<Void> modify(EmpParam.ModifyParam param){

        SysDept dept = deptMapper.selectById(param.getDeptId());
        if(dept==null){
            return Ref.error("错误的部门编号");
        }

        SysEmp emp = empMapper.selectById(param.getEmpId());
        if(emp==null){
            return Ref.error("错误的人员编号");
        }
        emp.setPost(param.getPost());
        emp.setPhoneNum(param.getPhoneNum());
        emp.setName(param.getName());
        emp.setDeptId(param.getDeptId());
        empMapper.updateById(emp);
        return Ref.ok();
    }

    /**
     * 删除人员
     */
    public CommonResult<Void> delete(EmpParam.DeleteParam param){

        //先判断是否绑定了用户,如果有用户.不能删除
        SysMember member = memberMapper.getByEmpId(param.getEmpId());
        if(member!=null){
            return Ref.error("绑定了用户,不能删除");
        }

        SysEmp emp = empMapper.selectById(param.getEmpId());
        if(emp==null){
            return Ref.error("错误的人员编号");
        }
        empMapper.deleteById(emp.getId());
        return Ref.ok();
    }

    /**
     * 条件查询
     */
    public CommonResult<DataGrid<EmpVo.ListQueryVo>> listQuery(EmpParam.ListQueryParam param){

        MemberInfo info = SystemUtil.getInfo();
        List<SysEmp> emps = empMapper.listQuery(param.getStart(), param.getNum(),
                param.getDeptId(), param.getKey(),info.getDeptIdList());
        int count = empMapper.countListQuery(param.getDeptId(),param.getKey(),
                info.getDeptIdList());

        List<EmpVo.ListQueryVo> vos = emps.stream().map(emp -> {
            EmpVo.ListQueryVo vo = new EmpVo.ListQueryVo();
            vo.setCreateTime(TimeUtil.formater(emp.getCreateTime()));
            vo.setDeptId(emp.getDeptId());
            vo.setEmpId(emp.getId());

            vo.setName(emp.getName());
            vo.setPhoneNum(emp.getPhoneNum());
            vo.setPost(emp.getPost());
            SysDept dept = deptMapper.selectById(emp.getDeptId());
            if(dept!=null){
                vo.setDeptName(dept.getShortName());
            }else{
                vo.setDeptName("");
            }
            SysMember member = memberMapper.getByEmpId(emp.getId());
            if(member!=null){
                vo.setMemberId(member.getId());
                vo.setMemberName(member.getUsername());
            }else{
                vo.setMemberName("");
            }
            return vo;
        }).collect(Collectors.toList());

        DataGrid<EmpVo.ListQueryVo> dataGrid = new DataGrid<>();
        dataGrid.setTotal(count);
        dataGrid.setRows(vos);
        return Ref.ok(dataGrid);
    }

}
